package com.service;


import com.dao.MenuDao;
import com.dao.RoleMenuDao;
import com.util.AjaxResult;
import com.util.UtilCheck;
import com.util.UtilRandom;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MenuService {

    @Autowired
    MenuDao menuDao;

    @Autowired
    RoleMenuDao roleMenuDao;

    public AjaxResult save(String pid,String menu_name,String menu_icon,String menu_uri,String menu_type,String menu_order){
        try {
            if(UtilCheck.isEmpty(pid)){
                pid = "0";
            }
            if(UtilCheck.isEmpty(menu_order)){
                Integer max = menuDao.getMax(pid);
                if(max == null || max < 1){
                    max = 0;
                }
                menu_order = String.valueOf(max+1);
            }
            Map<String,Object> param = new HashMap<>();
            param.put("menu_id", UtilRandom.uuid());
            param.put("menu_name",menu_name);
            param.put("menu_icon",menu_icon);
            param.put("pid",pid);
            param.put("menu_uri",menu_uri);
            param.put("menu_type",menu_type);
            param.put("menu_order",menu_order);
            int ck = menuDao.save(param);
            if(ck > 0){
                return AjaxResult.getOk();
            }
        } catch (Exception e) {
            if(e.getMessage().contains("menu_pid_IDX")){
                return AjaxResult.getFail("同级菜单【"+menu_name+"】存在");
            }
            return AjaxResult.getFail(e.getMessage());
        }
        return AjaxResult.getFail();
    }

    public AjaxResult update(String menu_id,String pid,String menu_name,String menu_icon,String menu_uri,String menu_type,String menu_order){
        try {
            if(UtilCheck.isEmpty(pid)){
                pid = "0";
            }
            if(UtilCheck.isEmpty(menu_order)){
                Integer max = menuDao.getMax(pid);
                if(max == null || max < 1){
                    max = 0;
                }
                menu_order = String.valueOf(max+1);
            }
            Map<String,Object> param = new HashMap<>();
            param.put("menu_id",menu_id);
            param.put("menu_name",menu_name);
            param.put("menu_icon",menu_icon);
            param.put("pid",pid);
            param.put("menu_uri",menu_uri);
            param.put("menu_type",menu_type);
            param.put("menu_order",menu_order);
            int ck = menuDao.update(param);
            if(ck > 0){
                return AjaxResult.getOk();
            }
        } catch (Exception e) {
            if(e.getMessage().contains("menu_pid_IDX")){
                return AjaxResult.getFail("同级菜单【"+menu_name+"】存在");
            }
            return AjaxResult.getFail(e.getMessage());
        }
        return AjaxResult.getFail();
    }

    public AjaxResult del(String menu_id) {
        try {
            int ck = menuDao.del(menu_id);
            if(ck > 0){
                return AjaxResult.getOk();
            }
        } catch (Exception e) {
            return AjaxResult.getFail(e.getMessage());
        }
        return AjaxResult.getFail();
    }

    public AjaxResult list(String menu_name,String menu_uri) {
        List<Map<String,Object>> list = menuDao.list(menu_name,menu_uri,null);
        List<Map<String,Object>> listResult = new ArrayList<>();
        if(!list.isEmpty()){
            List<Map<String,Object>> listRoot = list.stream().filter(g->"0".equals(g.get("pid").toString())).collect(Collectors.toList());
            if(listRoot.isEmpty()){
               //查询上级
               List<String> listId = list.stream().map(g->g.get("pid").toString()).collect(Collectors.toList());
               listRoot = menuDao.list("","",listId);
               listRoot = listRoot.stream().sorted(Comparator.comparingInt(g->Integer.valueOf(g.get("menu_order").toString()))).collect(Collectors.toList());
            }
            for(Map<String,Object> node1:listRoot){
                String menu_id = node1.get("menu_id").toString();
                List<Map<String,Object>> list2 = list.stream().filter(g->menu_id.equals(g.get("pid").toString())).collect(Collectors.toList());
                list2 = list2.stream().sorted(Comparator.comparingInt(g->Integer.valueOf(g.get("menu_order").toString()))).collect(Collectors.toList());
                node1.put("children",list2);
                listResult.add(node1);
            }
        }
        return AjaxResult.getOk(listResult);
    }

    public AjaxResult selectAll() {
        List<Map<String,Object>> list = menuDao.selectAll();
        List<Map<String,Object>> listResult = new ArrayList<>();
        if(!list.isEmpty()){
            List<Map<String,Object>> list2 = list.stream().filter(g->"0".equals(g.get("pid").toString())).collect(Collectors.toList());
            for(Map<String,Object> node1:list2){
                String menu_id = node1.get("menu_id").toString();
                List<Map<String,Object>> list3 = list.stream().filter(g->menu_id.equals(g.get("pid").toString())).collect(Collectors.toList());
                node1.put("children",list3);
                listResult.add(node1);
            }
        }
        return AjaxResult.getOk(listResult);
    }
}
